//
//  DBManage.m
//  Doctor
//
//  Created by duwen on 13-1-16.
//  Copyright (c) 2013年 duwen. All rights reserved.
//

#import "DBManage.h"
static long int numArr=0;
@implementation DBManage
-(id)init{
    if ((self=[super init])) {
        [self createEditableCopyOfDatabaseIfNeeded];
    }
    return self;
}

-(void)dealloc{
    [db close];

}

-(void)createEditableCopyOfDatabaseIfNeeded{
    BOOL success;
    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSError *error;
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
    NSString *documentsDirectory=[paths objectAtIndex:0];
    NSString *writableDBPath=[documentsDirectory stringByAppendingPathComponent:DB_NAME];
    success=[fileManager fileExistsAtPath:writableDBPath];
    

    
    if (!success) {
        NSString *defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DB_NAME];
        success=[fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

    }
    
    db=[FMDatabase databaseWithPath:writableDBPath];
    [db setTraceExecution:NO];
    [db open];
    if (![db open]) {
    }
    [db executeUpdate:@"CREATE TABLE IF NOT EXISTS NewCollect (PubDate text, Title text,Url text,Content Text,Thumbnail text,Channel text)"];
    [db executeUpdate:@"CREATE TABLE IF NOT EXISTS OffLine (Channel_id text, Id text,Newstitle text,Substitle text,Time text,Order text,Name text,Address text,Category text,Videourl text,Code text,From text,Help text,Content text,Editor text,Attendees text)"];
    
//    [db executeUpdate:@"CREATE TABLE IF NOT EXISTS Cache(Attendees text,Image text,Code text,From text,Channel_id text,Url text,Content text,File_id text,Substitle text,Videourl text,Category text,Editor text,Help text,Time text,Order text,Newstitle text,Name text,Type text,Channel_Title text)"];
}

//-(void)insertCache:(NSArray *)cache{
//    (.+?)NSLog(.+?)([\n\r])(@"cache__%@",cache);
//    [db executeUpdate:@"INSERT INTO Cache (Attendees,Image,Code,From,Channel_id,Url,Content,File_id,Substitle,Videourl,Category,Editor,Help,Time,Order,Newstitle,Name,Type,Channel_Title) VALUES (?,?,?,? ,?,?,?,? ,?,?,?,? ,?,?,?,? ,?,?,?)",[cache objectAtIndex:0],[cache objectAtIndex:1],[cache objectAtIndex:2],[cache objectAtIndex:3],[cache objectAtIndex:4],[cache objectAtIndex:5],[cache objectAtIndex:6],[cache objectAtIndex:7],[cache objectAtIndex:8],[cache objectAtIndex:9],[cache objectAtIndex:10],[cache objectAtIndex:11],[cache objectAtIndex:12],[cache objectAtIndex:13],[cache objectAtIndex:14],[cache objectAtIndex:15],[cache objectAtIndex:16],[cache objectAtIndex:17],[cache objectAtIndex:18]];
//}
//
//-(NSMutableArray *)cache{
//    
//    NSString *attendees;
//    NSString *image;
//    NSString *code;
//    NSString *from;
//    NSString *channel_id;
//    NSString *url;
//    
//    
//    NSString *content;
//    NSString *file_id;
//    NSString *substitle;
//    NSString *videourl;
//    NSString *category;
//    NSString *editor;
//    
//    
//    NSString *help;
//    NSString *time;
//    NSString *order;
//    NSString *newstitle;
//    NSString *name;
//    NSString *type;
//    NSString *channel_Title;
//    
//    NSMutableArray *goodsArr = [[NSMutableArray alloc]init];
//    NSDictionary *goodsDic;
//    FMResultSet *rs;
//    rs=[db executeQuery:@"SELECT * FROM Cache"];
//    while ([rs next]) {
//        attendees = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Attendees"]];
//        image =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Image"]];
//        code =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Code"]];
//        from =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"From"]];
//        channel_id =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Channel_id"]];
//        url = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Url"]];
//
//        content = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Content"]];
//        file_id =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"File_id"]];
//        substitle =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Substitle"]];
//        videourl =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Videourl"]];
//        category =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Category"]];
//        editor = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Editor"]];
//
//        help = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Help"]];
//        time =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Time"]];
//        order =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Order"]];
//        newstitle =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Newstitle"]];
//        name =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Name"]];
//        type = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Type"]];
//        channel_Title = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Channel_Title"]];
//        
//        goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: attendees,@"attendees",image,@"image",code,@"code",from,@"from",channel_id,@"channel_id", url,@"url",content,@"content",file_id,@"file_id",substitle,@"substitle",videourl,@"videourl",category,@"category", editor,@"editor",help,@"help",time,@"time",order,@"order",newstitle,@"newstitle",name,@"name",type,@"type",channel_Title,@"channel_Title",nil];
//        [goodsArr addObject:goodsDic];
//    }
//    
//    [rs close];
//    return goodsArr;
//
//}
-(void)deleteData:(NSString *)type whthChannel:(NSString *)channel_id wihtTitle:(NSString *)title{
    
}

-(void)insertData:(NSArray *)offLine{
    [db executeUpdate:@"INSERT INTO OffLine (Channel_id,Id,Newstitle,Substitle,Time,Order,Name,Address,Category,Videourl,Code,From,Help,Content,Editor,Attendees) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[offLine objectAtIndex:0],[offLine objectAtIndex:1],[offLine objectAtIndex:2],[offLine objectAtIndex:3],[offLine objectAtIndex:4],[offLine objectAtIndex:5],[offLine objectAtIndex:6],[offLine objectAtIndex:7],[offLine objectAtIndex:8],[offLine objectAtIndex:9],[offLine objectAtIndex:10],[offLine objectAtIndex:11],[offLine objectAtIndex:12],[offLine objectAtIndex:13],[offLine objectAtIndex:14],[offLine objectAtIndex:15]];
}

-(void)insertGoodsData:(NSArray *)goodsArr{
    [db executeUpdate:@"INSERT INTO NewCollect (PubDate,Title,Url,Content,Thumbnail,Channel) VALUES (?,?,?,?,?,?)",[goodsArr objectAtIndex:0],[goodsArr objectAtIndex:1],[goodsArr objectAtIndex:2],[goodsArr objectAtIndex:3],[goodsArr objectAtIndex:4],[goodsArr objectAtIndex:5]];
}
-(void)SelcetDate:(NSString *)title withChannel:(NSString *)channel{

    [db executeUpdate:@"SELECT NewCollect SET Title = ?  WHERE Channel = ?",title,channel];
}
-(void)deleteData:(NSString *)title{
    [db executeUpdate:@"DELETE FROM NewCollect WHERE Title = ?",[NSString stringWithFormat:@"%@",title]];
}
-(NSMutableArray *)selectNewCollect:(NSString *)titleStr withChannel:(NSString *)channeStr{

    NSString *pubdate;
    NSString *title;
    NSString *url;
    NSString *content;
    NSString *thumbnail;
    NSString *channel;
    NSMutableArray *dateArray=[[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM NewCollect"];
    while ([rs next]) {
        NSString* titlestr= [rs stringForColumn:@"Thumbnail"];
        NSString* channelstr = [rs stringForColumn:@"Channel"];
//        if ([channelstr isEqualToString:channeStr]) {
            if ([titlestr isEqualToString:titleStr]) {
                pubdate = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"PubDate"]];
                title =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Title"]];
                url =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Url"]];
                content =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Content"]];
                
                
                thumbnail =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Thumbnail"]];
                channel = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Channel"]];
                
                
                
                goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys:pubdate,@"pubdate",title,@"title",url,@"url",content,@"content",thumbnail,@"thumbnail", channel,@"channel",nil];
                [dateArray addObject:goodsDic];
//            }
            
        }
    }
    [rs close];

    
    return dateArray;

}
-(NSMutableArray*)Judgment:(NSString *)timerr
{

    NSString *pubdate;
    NSString *title;
    NSString *url;
    NSString *content;
    NSString *thumbnail;
    NSString *channel;
    
    NSMutableArray *dateArray=[[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM GoodsTable"];
    while ([rs next]) {
        NSString* time = [rs stringForColumn:@"pubdate"];
       
        if ([time isEqualToString:timerr]) {

            pubdate = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"PubDate"]];
            title =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Title"]];
            url =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Url"]];
            content =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Content"]];
            
           
            thumbnail =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Thumbnail"]];
            channel = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Channel"]];
            
            

            goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys:pubdate,@"pubdate",title,@"title",url,@"url",content,@"content",thumbnail,@"thumbnail", channel,@"channel",nil];
            [dateArray addObject:goodsDic];
        }else{
            break;
        }
    }
    [rs close];
    return dateArray;
}
-(void)updataBase:(NSString *)Phao Andzaiku:(NSString *)zaiku Andbeizhu:(NSString*)beizhu{

    [db executeUpdate:@"UPDATE GoodsTable SET Shifouzaiku = ? , Beizhu = ? WHERE Pihao = ?",zaiku,beizhu,Phao];
}
-(NSMutableArray*)weichaArr:(NSString*)kumingcheng{
    NSString *xuhao;
    NSString *pihao;
    NSString *huoquanren;
    NSString *dengji;
    NSString *chandi;
    
    NSString *jianshu;
    NSString *gongdingzhong;
    NSString *jiagongdanwei;
    NSString *beizhu;
    NSString *cangkubianhao;
    
    NSString *cangkumingcheng;
    NSString *shifouzaiku;
    NSString *zhaopianurlmax;
    NSString *zhaopianurlmin;
    
    
    NSMutableArray *dateArray=[[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM GoodsTable"];
    while ([rs next]) {
        NSString* time = [rs stringForColumn:@"cangkumingcheng"];
        
        if ([time isEqualToString:kumingcheng]) {
            pihao =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Pihao"]];
            huoquanren = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Huoquanren"]];
            dengji =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Dengji"]];
            chandi =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Chandi"]];
            xuhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Xuhao"]];
            
            
            jianshu =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jianshu"]];
            gongdingzhong = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Gongdingzhong"]];
            jiagongdanwei =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jiagongdanwei"]];
            beizhu =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Beizhu"]];
            cangkubianhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkubianhao"]];
            
            cangkumingcheng =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkumingcheng"]];
            shifouzaiku = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Shifouzaiku"]];
            zhaopianurlmax =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmax"]];
            zhaopianurlmin =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmin"]];
            
            
            goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: pihao,@"pihao", huoquanren,@"huoquanren",dengji,@"dengji",chandi,@"chandi",xuhao,@"xuhao",jianshu,@"jianshu", gongdingzhong,@"gongdingzhong",jiagongdanwei,@"jiagongdanwei",beizhu,@"beizhu",cangkubianhao,@"cangkubianhao",cangkumingcheng,@"cangkumingcheng", shifouzaiku,@"shifouzaiku",zhaopianurlmax,@"zhaopianurlmax",zhaopianurlmin,@"zhaopianurlmin",nil];
            [dateArray addObject:goodsDic];
        }
    }
    [rs close];
    return dateArray;
}


-(NSMutableArray*)chaku:(NSString*)tate{
    NSString *xuhao;
    NSString *pihao;
    NSString *huoquanren;
    NSString *dengji;
    NSString *chandi;
    
    NSString *jianshu;
    NSString *gongdingzhong;
    NSString *jiagongdanwei;
    NSString *beizhu;
    NSString *cangkubianhao;
    
    NSString *cangkumingcheng;
    NSString *shifouzaiku;
    NSString *zhaopianurlmax;
    NSString *zhaopianurlmin;
    
    
    NSMutableArray *dateArray=[[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM GoodsTable"];
    while ([rs next]) {
        NSString* time = [rs stringForColumn:@"pihao"];

        if ([time isEqualToString:tate]) {
            pihao =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Pihao"]];
            huoquanren = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Huoquanren"]];
            dengji =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Dengji"]];
            chandi =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Chandi"]];
            xuhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Xuhao"]];
            
            
            jianshu =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jianshu"]];
            gongdingzhong = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Gongdingzhong"]];
            jiagongdanwei =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jiagongdanwei"]];
            beizhu =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Beizhu"]];
            cangkubianhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkubianhao"]];
            
            cangkumingcheng =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkumingcheng"]];
            shifouzaiku = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Shifouzaiku"]];
            zhaopianurlmax =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmax"]];
            zhaopianurlmin =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmin"]];
            
            
            goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: pihao,@"pihao", huoquanren,@"huoquanren",dengji,@"dengji",chandi,@"chandi",xuhao,@"xuhao",jianshu,@"jianshu", gongdingzhong,@"gongdingzhong",jiagongdanwei,@"jiagongdanwei",beizhu,@"beizhu",cangkubianhao,@"cangkubianhao",cangkumingcheng,@"cangkumingcheng", shifouzaiku,@"shifouzaiku",zhaopianurlmax,@"zhaopianurlmax",zhaopianurlmin,@"zhaopianurlmin",nil];
            [dateArray addObject:goodsDic];
        }
    }
    [rs close];
    return dateArray;
}

-(NSMutableArray*)shangchuanArr:(NSString*)bianhao{
    NSString *xuhao;
    NSString *pihao;
    NSString *huoquanren;
    NSString *dengji;
    NSString *chandi;
    
    NSString *jianshu;
    NSString *gongdingzhong;
    NSString *jiagongdanwei;
    NSString *beizhu;
    NSString *cangkubianhao;
    
    NSString *cangkumingcheng;
    NSString *shifouzaiku;
    NSString *zhaopianurlmax;
    NSString *zhaopianurlmin;
    
    
    NSMutableArray *dateArray=[[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM GoodsTable"];
    while ([rs next]) {
        NSString* time = [rs stringForColumn:@"cangkubianhao"];

        if ([time isEqualToString:bianhao]) {
            pihao =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Pihao"]];
            huoquanren = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Huoquanren"]];
            dengji =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Dengji"]];
            chandi =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Chandi"]];
            xuhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Xuhao"]];
            
            
            jianshu =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jianshu"]];
            gongdingzhong = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Gongdingzhong"]];
            jiagongdanwei =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jiagongdanwei"]];
            beizhu =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Beizhu"]];
            cangkubianhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkubianhao"]];
            
            cangkumingcheng =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkumingcheng"]];
            shifouzaiku = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Shifouzaiku"]];
            zhaopianurlmax =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmax"]];
            zhaopianurlmin =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmin"]];
            
            
            goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: pihao,@"pihao", huoquanren,@"huoquanren",dengji,@"dengji",chandi,@"chandi",xuhao,@"xuhao",jianshu,@"jianshu", gongdingzhong,@"gongdingzhong",jiagongdanwei,@"jiagongdanwei",beizhu,@"beizhu",cangkubianhao,@"cangkubianhao",cangkumingcheng,@"cangkumingcheng", shifouzaiku,@"shifouzaiku",zhaopianurlmax,@"zhaopianurlmax",zhaopianurlmin,@"zhaopianurlmin",nil];
            [dateArray addObject:goodsDic];
        }
    }
    [rs close];
    return dateArray;
}

-(NSMutableArray*)searchArr:(NSString*)phao{
    NSString *xuhao;
    NSString *pihao;
    NSString *huoquanren;
    NSString *dengji;
    NSString *chandi;
    
    NSString *jianshu;
    NSString *gongdingzhong;
    NSString *jiagongdanwei;
    NSString *beizhu;
    NSString *cangkubianhao;
    
    NSString *cangkumingcheng;
    NSString *shifouzaiku;
    NSString *zhaopianurlmax;
    NSString *zhaopianurlmin;
    
    
    NSMutableArray *dateArray=[[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM GoodsTable"];
    while ([rs next]) {
        NSString* time = [rs stringForColumn:@"pihao"];

        if ([time isEqualToString:phao]) {
            pihao =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Pihao"]];
            huoquanren = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Huoquanren"]];
            dengji =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Dengji"]];
            chandi =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Chandi"]];
            xuhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Xuhao"]];
            
            
            jianshu =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jianshu"]];
            gongdingzhong = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Gongdingzhong"]];
            jiagongdanwei =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Jiagongdanwei"]];
            beizhu =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Beizhu"]];
            cangkubianhao =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkubianhao"]];
            
            cangkumingcheng =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Cangkumingcheng"]];
            shifouzaiku = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Shifouzaiku"]];
            zhaopianurlmax =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmax"]];
            zhaopianurlmin =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Zhaopianurlmin"]];
            
            
            goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: pihao,@"pihao", huoquanren,@"huoquanren",dengji,@"dengji",chandi,@"chandi",xuhao,@"xuhao",jianshu,@"jianshu", gongdingzhong,@"gongdingzhong",jiagongdanwei,@"jiagongdanwei",beizhu,@"beizhu",cangkubianhao,@"cangkubianhao",cangkumingcheng,@"cangkumingcheng", shifouzaiku,@"shifouzaiku",zhaopianurlmax,@"zhaopianurlmax",zhaopianurlmin,@"zhaopianurlmin",nil];
            [dateArray addObject:goodsDic];
        }else{
            break;
        }
    }
    [rs close];
    return dateArray;
}

-(void)insertGoodsPihao:(NSString*)gPid AndUrl:(NSString * )url{
    [db executeUpdate:@"UPDATE GoodsTable SET Cangkumingcheng = '1',Zhaopianurlmin = ? where Pihao = ?",gPid,url];
}
//个数
-(void)insertGoodsPihao:(NSString*)gPid{

//    FMResultSet *rs = [db executeQuery:@"SELECT * FROM GoodsTable"];
//    while ([rs next]) {
//        NSString * pihao = [rs stringForColumn:@"Pihao"];
    
//        NSString * oldBeizhu = [rs stringForColumn:@"Beizhu"];
//        if (gPid==pihao) {

            [db executeUpdate:@"UPDATE GoodsTable SET Cangkumingcheng = '1' where Pihao = ?",gPid];
//        }
//    }
//    [rs close];
}
-(void)updateCang:(NSString *)pihao AndmaxUrl:(NSString*)max AndminUrl:(NSString*)min Andbianhao:(NSString*)bian{

    [db executeUpdate:@"UPDATE GoodsTable SET Zhaopianurlmax = ?,Zhaopianurlmin = ?, Cangkubianhao = ? where Pihao = ?",pihao,max,min,bian];
}

-(void)updateCang:(NSString *)pihao AndmaxUrl:(NSString*)max{
    [db executeUpdate:@"UPDATE GoodsTable SET Zhaopianurlmax = ? where Pihao = ?",pihao,max];
}

//判断批号是否存在
-(BOOL)findPihaoEqualToNpihao:(NSString*)newPihao{
    FMResultSet *rs;
    rs = [db executeQuery:@"SELECT * FROM NewCollect"];
    while ([rs next]) {
        NSString * locaPi = [rs stringForColumn:@"Title"];
        if (locaPi == newPihao) {
            return YES;
        }

    }
    [rs close];
    return NO;
}
-(NSMutableArray *)getOffLine{
//    id newstitle substitle time order name address category videourl code from help content editor attendees

    NSString *channel_id;
    NSString *_id;
    NSString *newstitle;
    NSString *substitle;
    NSString *time;
    NSString *order;

    NSString *name;
    NSString *address;
    NSString *category;
    NSString *videourl;
    NSString *code;
    NSString *from;

    NSString *help;
    NSString *content;
    NSString *editor;
    NSString *attendees;
    
    NSMutableArray *goodsArr = [[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs;
    rs=[db executeQuery:@"SELECT * FROM OffLine"];
    while ([rs next]) {
        channel_id = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Channel_id"]];
        _id =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Id"]];
        newstitle =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Newstitle"]];
        substitle =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Substitle"]];
        time =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Time"]];
        order = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Order"]];
        
        name = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Name"]];
        address =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Address"]];
        category =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Category"]];
        videourl =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Videourl"]];
        code =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Code"]];
        from = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"From"]];
        
        help = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Help"]];
        content =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Content"]];
        editor =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Editor"]];
        attendees =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Attendees"]];
        
        goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: channel_id,@"channel_id",_id,@"id",newstitle,@"newstitle",substitle,@"substitle",time,@"time", order,@"order",name,@"name",address,@"address",category,@"category",videourl,@"videourl",code,@"code",from,@"from",help,@"help",content,@"content",editor,@"editor",attendees,@"attendees",nil];
        [goodsArr addObject:goodsDic];
    }

    [rs close];
    return goodsArr;
}
-(NSMutableArray *)goodsDetialArr{
    NSString *pubdate;
    NSString *title;
    NSString *url;
    NSString *content;
    
    NSString *thumbnail;
    NSString *channel;
    
    NSMutableArray *goodsArr = [[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs;
    rs=[db executeQuery:@"SELECT * FROM NewCollect"];
    while ([rs next]) {
        pubdate = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"PubDate"]];
        title =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Title"]];
        url =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Url"]];
        content =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Content"]];
        
        
        thumbnail =  [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Thumbnail"]];
        channel = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Channel"]];
        
        goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: pubdate,@"pubdate",title,@"title",url,@"url",content,@"content",thumbnail,@"thumbnail", channel,@"channel",nil];
        [goodsArr addObject:goodsDic];
    }

    [rs close];
    return goodsArr;
    
}
-(NSMutableArray *)goodNumArray:(int)Num
{
    NSString *timer;
    NSString *systolic;
    NSString *dlastoloc;
    NSString *pulse;
    NSString *date;
    NSMutableArray *goodsArr = [[NSMutableArray alloc]init];
    NSDictionary *goodsDic;
    FMResultSet *rs;
    rs=[db executeQuery:@"SELECT * FROM GoodsTable order by Date desc"];
    while ([rs next]) {

        if(numArr!=Num){
        systolic =  [NSString stringWithFormat:@"%@",[rs  stringForColumn:@"Systolic"]];
        dlastoloc = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Dlastoloc"]];
        pulse =     [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Pulse"]];
        date =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Date"]];
        timer =      [NSString stringWithFormat:@"%@",[rs stringForColumn:@"Timer"]];
        goodsDic =  [[NSDictionary alloc]initWithObjectsAndKeys: systolic,@"systolic", dlastoloc,@"dlastoloc",pulse,@"pulse",date,@"date",timer,@"time", nil];
        [goodsArr addObject:goodsDic];
            numArr++;
        }else{
            numArr=0;
            break;
        }
    }

    [rs close];
    return goodsArr;

}
-(void)deleteGoodsInfoInGoodsTable:(NSString*)Title{
    [db executeUpdate:@"DELETE FROM NewCollect WHERE Title=?",[NSString stringWithFormat:@"%@",Title]];

}


@end
